package lxs.swift.collector;

import lxs.swift.tool.Formatter;

public class AppError extends RuntimeException {
    private static final long serialVersionUID = 1L;
    private final boolean isError;
    private int code;
    private Object data;

    private AppError(boolean isError, int code, String message, Object data) {
        super(message);
        this.isError = isError;
        this.code = code;
        this.data = data;
    }

    public boolean isError() {
        return isError;
    }

    public int getCode() {
        return code;
    }

    public Object getData() {
        return data;
    }

    public AppError code(int code) {
        this.code = code;
        return this;
    }

    public AppError data(Object data) {
        this.data = data;
        return this;
    }

    public static AppError ok(Object data, String messageTemplate, Object... values) {
        String message = Formatter.fmt(messageTemplate, values);
        return new AppError(false, 200, message, data);
    }

    public static AppError ok(Object data) {
        return new AppError(false, 200, "", data);
    }

    public static AppError error(String messageTemplate, Object... values) {
        String message = Formatter.fmt(messageTemplate, values);
        return new AppError(true, 500, message, null);
    }

    public static AppError error(int code, String messageTemplate, Object... values) {
        String message = Formatter.fmt(messageTemplate, values);
        return new AppError(true, code, message, null);
    }

    public static AppError warn(int code, String messageTemplate, Object... values) {
        String message = Formatter.fmt(messageTemplate, values);
        return new AppError(false, code, message, null);
    }

    public static AppError warn(String messageTemplate, Object... values) {
        String message = Formatter.fmt(messageTemplate, values);
        return new AppError(false, 400, message, null);
    }


}
